package com.imooc.coupon.controller;

import com.alibaba.fastjson.JSON;
import com.imooc.coupon.exception.CouponException;
import com.imooc.coupon.executor.ExecutorManager;
import com.imooc.coupon.support.vo.SettlementInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * 优惠券结算微服务控制器
 *
 * Created By TINY
 * Create Time 2020/2/12
 **/
@Slf4j
@RestController
public class SettlementController {

    /* 优惠券结算规则执行管理器 */
    private final ExecutorManager executorManager;

    @Autowired
    public SettlementController(ExecutorManager executorManager) {
        this.executorManager = executorManager;
    }

    /**
     * 优惠券结算服务接口
     *
     * @param settlement {@link SettlementInfo} 结算信息
     * @return {@link SettlementInfo} 经过计算处理后的结算信息
     * @throws CouponException 业务异常
     */
    @PostMapping("/settlement/compute")
    public SettlementInfo computeRule(@RequestBody SettlementInfo settlement)
            throws CouponException {
        log.info("对（{}）执行优惠券结算", JSON.toJSONString(settlement));

        return executorManager.computeRule(settlement);
    }
}
